import 'package:flutter_chat/common/constant/constants.dart';
import 'package:flutter_chat/pages/index.dart';
import 'package:get/get.dart';

import '../middleware/auth_route_middleware.dart';

/// 注册路由
class AppPages {
  /// 初始页面
  static String initialRoute = RouterName.login;

  /// 定义路由
  static final List<GetPage<dynamic>> pages = [
    /// 登录页面
    getNeedAuthRoute(
      name: RouterName.login,
      page: () => const LoginPage(),
      binding: LoginBinding(),
    ),

    /// 注册页面
    getNeedAuthRoute(
      name: RouterName.register,
      page: () => const RegisterPage(),
      binding: RegisterBinding(),
    ),

    /// 首页
    getNeedAuthRoute(
      name: RouterName.index,
      page: () => const ApplicationPage(),
      bindings: [
        ApplicationBinding(),
        MessageBinding(),
        ContactsBinding(),
        UserBinding(),
      ],
    ),

    /// 用户聊天页面
    getNeedAuthRoute(
      name: "${RouterName.chatUser}/:${RouterParam.username}",
      page: () => ChatUserPage(),
      binding: ChatUserBinding(),
    ),

    /// 用户个人信息页面
    getNeedAuthRoute(
      name: RouterName.userInfo,
      page: () => const UserInfoPage(),
      binding: UserInfoBinding(),
    ),

    /// 设置页面
    getNeedAuthRoute(
      name: RouterName.setting,
      page: () => const SettingPage(),
      binding: SettingBinding(),
    ),

    /// 群聊页面
    getNeedAuthRoute(
      name: RouterName.groupList,
      page: () => const GroupListPage(),
      binding: GroupListBinding(),
    ),

    /// 聊天详情页面
    getNeedAuthRoute(
      name: "${RouterName.chatUserInfo}/:${RouterParam.chatId}",
      page: () => ChatUserInfoPage(),
      binding: ChatUserInfoBinding(),
    ),

    /// 用户主页
    getNeedAuthRoute(
      name: "${RouterName.userHome}/:${RouterParam.username}",
      page: () => UserHomePage(),
      binding: UserHomeBinding(),
    ),

    /// 关于我们
    getNeedAuthRoute(
      name: RouterName.about,
      page: () => const AboutPage(),
      binding: AboutBinding(),
    ),
  ];

  /// 封装需要登录的路由
  static GetPage<dynamic> getNeedAuthRoute({
    required String name,
    required GetPageBuilder page,
    Bindings? binding,
    List<Bindings> bindings = const [],
  }) {
    return GetPage(
      name: name,
      page: page,
      binding: binding,
      bindings: bindings,
      middlewares: [
        AuthRouteMiddleware(priority: 1),
      ],
    );
  }
}
